import java.util.Vector;

import javax.swing.JTable;

import org.scilab.modules.javasci.JavasciException;
import org.scilab.modules.javasci.Scilab;
import org.scilab.modules.javasci.JavasciException.InitializationException;
import org.scilab.modules.types.ScilabType;
import org.scilab.modules.types.ScilabTypeEnum;



/** 
 * 
 * ��Ŀ��ƣ�javaSci 
 * ����ƣ�SciUtils 
 * �������� 
 * �����ˣ�ruizhao 
 * ����ʱ�䣺2011-3-20 ����03:45:02 
 * �޸��ˣ�ruizhao 
 * �޸�ʱ�䣺2011-3-20 ����03:45:02 
 * �޸ı�ע�� 
 * @version 
 * 
 */

public class SciUtils {
	/*
	 * ����scilab ִ��
	 * param table.toString 
	 * 
	 * return String[][]
	 * */
	public static String[][] sciEngin(String tablemodel) {
		//String send = StringUtils.format4Table2sci(tablemodel);
		String[][] recive = null;
		ScilabType scitype = null;
		Scilab sci;
		try {
			sci = new Scilab();

			if (sci.open()) {
				sci.exec(tablemodel);
				if(sci.isExistingVariable(StringUtils.BEGIN)){
					scitype = sci.get(StringUtils.BEGIN);
					System.out.println("scitype:"+scitype.toString());
					recive = StringUtils.format4sci2table(scitype.toString(),
					scitype.getHeight(), scitype.getWidth());
				}
				sci.close();
			}

		} catch (InitializationException e) {
			e.printStackTrace();
		} catch (JavasciException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return recive;

	}
	/*
	 * ���������ֵ�����
	 * param Vector , String
	 * 
	 * return Vector
	 * */
	public static Vector sciEngin(Vector tablemodel,int op,String para) {
		int h=tablemodel.size();
		String send = StringUtils.format4Table2sci(tablemodel.toString());
		System.out.println("send is :"+send);
		switch (op){
		case 0 :
			send += "+"+ para;
			break;
		case 1:
			send += "-" + para;
			break;
		case 2:
			send += "*" + para;
			break;
		case 3:
			send += "/" + para;
			break;
		default:
			break;
		
		}
		Vector recive = null;
		ScilabType scitype = null;
		Scilab sci;
		try {
			sci = new Scilab();
			if (sci.open()) {
				System.out.println(send);
				sci.exec(send);
				if(sci.isExistingVariable(StringUtils.BEGIN)){
					
					 scitype= sci.get(StringUtils.BEGIN);
					System.out.println("scitype:"+scitype.toString());
					recive = (Vector)scitype;
				}
				sci.close();
			}

		} catch (InitializationException e) {
			e.printStackTrace();
		} catch (JavasciException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return recive;

	}

	public static boolean checkdata(JTable table){
		int hang = table.getRowCount();
		int lie = table.getColumnCount();
		for (int i = 0; i < hang; i++) {
			//��
			for (int j = 0; j < lie; j++) {
				String 	value = table.getModel().getValueAt(i, j).toString();
				if (value== null||!StringUtils.isNumber(value)) {
					int action = MessageBox.Show("是否自动将非数字位置为'0'?", 12);
					switch(action ) {
						case -1:
							table.selectAll();
							return false;
						case 0:
							table.getModel().setValueAt(0, i, j);
							break;
						case 2:
							table.selectAll();
							return false;
						default:
							break;
					}
				} 
			
			
			}
			
		}
		return true;
	}
	/*
	 * 
	 * Scitype2Vector;
	 * 
	 * */

}
